package org.itboys.mobile.controller.weixin;

import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.dto.SundayMemberSessionDto;
import org.itboys.mobile.dto.dynamic.SundayDynamicDto;
import org.itboys.mobile.service.mongo.front.dynamic.SundayFrontDynamicService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 作者：zhangyang
 * 时间：2017年12月18日
 * 描述：圣代_动态_controller_weixin
 */
@Controller
@RequestMapping("/sunday/weixin/dynamic")
public class SundayWeiXinDynamicController extends BaseController{

    @Autowired
    private SundayFrontDynamicService dynamicService;




    /**
     * 系统动态主页/个人动态主页
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping(value="/index", method= {RequestMethod.GET,RequestMethod.POST})
    public String index(
            HttpServletRequest request,
            HttpServletResponse response,
            Model model,
            @ApiParam(required=false, name="personId", value="用户ID。需要查看动态的用户ID")@RequestParam(value = "personId", required = false)Long personId
            ){
        try{
            //个人动态主页
            if(LongUtil.isNotNull(personId)){
                SundayMemberSessionDto memberSessionDto= AccountIdHoldUtils.getSessionMember(request);
                Map<String,Object> result = dynamicService.selectDynamicByPerson(request,memberSessionDto.getId(),personId);
                model.addAttribute("result",result);
                return "/aigou/weixin/dynamic/person/index";
            //系统动态主页
            }else{
                return "/aigou/weixin/dynamic/index";
            }

        }catch (Exception e){
            logger.error("/sunday/weixin/dynamic/index----e="+e.getMessage());
            e.printStackTrace();
            return "/aigou/weixin/error";
        }
    }


    @RequestMapping(value="/select", method={RequestMethod.GET,RequestMethod.POST})
    @ApiOperation("【微信端】获取分页动态数据:本地爱购/达人试用/特别关注")
    public String select(
            HttpServletRequest request,
            HttpServletResponse response,
            Model model,
            @ApiParam(required=true, name="type", value="类别:2-达人试用,3-本地爱购,4-特别关注")@RequestParam(value = "type", required = true)Integer type
            ){
        try{
          SundayMemberSessionDto memberSessionDto= AccountIdHoldUtils.getSessionMember(request);
            List<SundayDynamicDto> dynamics = dynamicService.selectDynamic(request,memberSessionDto.getId(), type);
            model.addAttribute("dynamics",dynamics);
            return "/aigou/weixin/dynamic/data";
        }catch (Exception e){
            logger.error("/sunday/weixin/dynamic/select----e="+e.getMessage());
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 动态详情
     * @param request
     * @param model
     * @param dynamicId
     * @return
     */
    @RequestMapping(value="/detail", method= {RequestMethod.GET,RequestMethod.POST})
    public String detail(HttpServletRequest request,
                         HttpServletResponse response,
                         Model model,
                               @RequestParam(value = "dynamicId", required = true)Long dynamicId){
        try{
            SundayMemberSessionDto memberSessionDto=AccountIdHoldUtils.getSessionMember(request);
            SundayDynamicDto dynamic = dynamicService.detail(dynamicId,memberSessionDto.getId());
            model.addAttribute("dynamic",dynamic);
            return "/aigou/weixin/dynamic/detail";
        }catch (Exception e){
            logger.error("/sunday/weixin/dynamic/detail----e="+e.getMessage());
            e.printStackTrace();
            return "/aigou/weixin/error";
        }
    }
    @RequestMapping(value="/operate", method= {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation(value="【微信端】点赞或评论")
    public void operate(HttpServletRequest request,
                        HttpServletResponse response,
                         @ApiParam(required=true, name="dynamicId", value="动态ID")@RequestParam(value = "dynamicId", required = true)Long dynamicId,
                         @ApiParam(required=true, name="type", value="类型。1点赞，2评论")@RequestParam(value = "type", required = true)Integer type,
                         @ApiParam(required=false, name="content", value="实体名称")@RequestParam(value = "content", required = false)String content){
        try{
            SundayMemberSessionDto memberSessionDto =AccountIdHoldUtils.getSessionMember(request);
            dynamicService.operate(dynamicId,memberSessionDto.getId(), type, content);
            ResponseMessage.success();
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code= CommonConstants.TOKEN_CODE;
                msg= CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/weixin/dynamic/operate----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }

}
